# 该部分集成了mysql数据库的增删改接口
from Flask_run import db
from database.models.message import Message
from database.models.mri import Mri
from database.models.patient import Patient


# 增 方法
# def sql_add():
#     add_result = Mri(study_id="1351232322",inv_pred="0",men_pred="1")
#     db.session.add(add_result)
#     db.session.commit()
    
# 改 方法
# def sql_update():    
#     db.session.query(TCL).filter_by(study_id=1351232322).update({'inv_pred': "1"})
#     db.session.commit()
  
# # 删 方法  
# def sql_delete():
    
#     tcl = db.session.query(TCL).filter(TCL.study_id=="1351232322").first()
#     db.session.delete(tcl)
#     db.session.commit()

def TCL_update(study_id,inv_pred,men_pred):
    # 更新
    if str(inv_pred)=="0":
      inv="否"
    elif str(inv_pred) == "1":
      inv="是"
    else:
      inv='错误'
    if str(men_pred)=="0":
      men="一级"
    elif str(men_pred)=="1":
      men='非一级'
    else:
      men='错误'
    db.session.query(Mri).filter_by(study_id=study_id).update({'ai_invasion': inv,'ai_level':men})
    db.session.commit()

    # 查找到医生判断的结果
    Mri_item = db.session.query(Mri.patholog_invasion,Mri.pathology_level).filter(Mri.study_id == study_id).all()
    Mri_item=Mri_item[0]

    if Mri_item[0] == None or Mri_item[0] == "":
      inv = ""
    else:
      if Mri_item[0] == inv:
        inv="是"
      else:
        inv="否" 
    
    if Mri_item[1] == None or Mri_item[1] == "":
      men = None
    else:
      if Mri_item[1]==men:
        men="是"
      else:
        men="否"



    db.session.query(Mri).filter_by(study_id=study_id).update({'inv_consistence': inv,'level_consistence': men})
    db.session.commit()
    
# def TCL_add(study_id,inv_pred,men_pred):
#     add_result = Mri(study_id=study_id,inv_pred=inv_pred,men_pred=men_pred)
#     db.session.add(add_result)
#     db.session.commit()

# db.create_all()
#TCL_add("1351232322","0","11")
#TCL_update("1351232322","0","1")